<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Account as AccountModel;
use app\common\model\Assets;
use app\common\tool\Result;
use think\Response;

class Account extends Base
{
    public function index(): Response
    {
        $account = Assets::getFields(3);
        $result = [];
        foreach ($account as $key => $value) {
            $result[] = [
                'title' => $value,
                'type' => $key,
                'balance' => $this->user->assets->$key,
                'url' => "/pages/account/record?type={$key}&title={$value}"
            ];
        }

        return Result::success('success', $result);
    }

    public function record(): Response
    {
        $param = $this->request->get();
        $type = !empty($param['type']) ? $param['type'] : 'bonus';
        $map['uid'] = $this->user->id;
        $map['type'] = $type;
        if (!empty($param['mode'])) {
            $map['fx'] = $param['mode'] == 1 ? '+' : '-';
        }
        if (!empty($param['is_bonus'])) {
            $map['is_bonus'] = $param['is_bonus'];
        }
        if (!empty($param['category'])) {
            $map['category'] = $param['category'];
        }
        $result = AccountModel::getPageList($map,
            'id,fx,amount,front,after,intro,source,create_time', $this->getPage(), $this->getLimit(), false, ['take']);
        $result['balance'] = $this->user->assets->$type;

        return Result::fastReturn($result);
    }
}